home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
c01lab4.zip
/
LRMRDR
/
LRM_CHAP.ZIP
/
CHAP06.DOC
< prev
next >
Wrap
Text File
|
1992-04-21
|
35KB
|
809 lines
> 6. Subprograms
Subprograms are one of the four forms of program unit, of which programs
can be composed. The other forms are packages, task units, and generic
units.
A subprogram is a program unit whose execution is invoked by a subprogram
call. There are two forms of subprogram: procedures and functions. A
procedure call is a statement; a function call is an expression and
returns a value. The definition of a subprogram can be given in two parts:
a subprogram declaration defining its calling conventions, and a subprogram
body defining its execution.
References: function 6.5, function call 6.4, generic unit 12, package 7,
procedure 6.1, procedure call 6.4, subprogram body 6.3, subprogram call
6.4, subprogram declaration 6.1, task unit 9
> 6.1 Subprogram Declarations
A subprogram declaration declares a procedure or a function, as indicated
by the initial reserved word.
subprogram_declaration ::= subprogram_specification;
subprogram_specification ::=
procedure identifier [formal_part]
| function designator [formal_part] return type_mark
designator ::= identifier | operator_symbol
operator_symbol ::= string_literal
formal_part ::=
(parameter_specification {; parameter_specification})
parameter_specification ::=
identifier_list : mode type_mark [:= expression]
mode ::= [in] | in out | out
The specification of a procedure specifies its identifier and its formal
parameters (if any). The specification of a function specifies its
designator, its formal parameters (if any) and the subtype of the returned
value (the result subtype). A designator that is an operator symbol is
used for the overloading of an operator. The sequence of characters
represented by an operator symbol must be an operator belonging to one of
the six classes of overloadable operators defined in section 4.5 (extra
spaces are not allowed and the case of letters is not significant).
A parameter specification with several identifiers is equivalent to a
sequence of single parameter specifications, as explained in section 3.2.
Each single parameter specification declares a formal parameter. If no
mode is explicitly given, the mode in is assumed. If a parameter
specification ends with an expression, the expression is the default
expression of the formal parameter. A default expression is only allowed
in a parameter specification if the mode is in (whether this mode is
indicated explicitly or implicitly). The type of a default expression must
be that of the corresponding formal parameter.
The use of a name that denotes a formal parameter is not allowed in default
expressions of a formal part if the specification of the parameter is
itself given in this formal part.
The elaboration of a subprogram declaration elaborates the corresponding
formal part. The elaboration of a formal part has no other effect.
Examples of subprogram declarations:
procedure TRAVERSE_TREE;
procedure INCREMENT(X : in out INTEGER);
procedure RIGHT_INDENT(MARGIN : out LINE_SIZE); -- see 3.5.4
procedure SWITCH(FROM, TO : in out LINK); -- see 3.8.1
function RANDOM return PROBABILITY; -- see 3.5.7
function MIN_CELL(X : LINK) return CELL; -- see 3.8.1
function NEXT_FRAME(K : POSITIVE) return FRAME; -- see 3.8
function DOT_PRODUCT(LEFT,RIGHT: VECTOR) return REAL; -- see 3.6
function "*"(LEFT,RIGHT : MATRIX) return MATRIX; -- see 3.6
Examples of in parameters with default expressions:
procedure PRINT_HEADER(PAGES : in NATURAL;
HEADER : in LINE := (1 .. LINE'LAST => ' '); -- see 3.6
CENTER : in BOOLEAN := TRUE);
Notes:
The evaluation of default expressions is caused by certain subprogram
calls, as described in section 6.4.2 (default expressions are not evaluated
during the elaboration of the subprogram declaration).
All subprograms can be called recursively and are reentrant.
References: declaration 3.1, elaboration 3.9, evaluation 4.5, expression
4.4, formal parameter 6.2, function 6.5, identifier 2.3, identifier list
3.2, mode 6.2, name 4.1, elaboration has no other effect 3.9, operator 4.5,
overloading 6.6 8.7, procedure 6, string literal 2.6, subprogram call 6.4,
type mark 3.3.2
> 6.2 Formal Parameter Modes
The value of an object is said to be read when this value is evaluated; it
is also said to be read when one of its subcomponents is read. The value
of a variable is said to be updated when an assignment is performed to the
variable, and also (indirectly) when the variable is used as actual
parameter of a subprogram call or entry call statement that updates its
value; it is also said to be updated when one of its subcomponents is
updated.
A formal parameter of a subprogram has one of the three following modes:
in The formal parameter is a constant and permits only reading of the value of the
associated actual parameter.
in out The formal parameter is a variable and permits both reading and updating of the value
of the associated actual parameter.
out The formal parameter is a variable and permits updating of the value of the associated
actual parameter.
The value of a scalar parameter that is not updated by the call is undefined upon return;
the same holds for the value of a scalar subcomponent, other than a discriminant.
Reading the bounds and discriminants of the formal parameter and of its subcomponents
is allowed, but no other reading.
For a scalar parameter, the above effects are achieved by copy: at the
start of each call, if the mode is in or in out, the value of the actual
parameter is copied into the associated formal parameter; then after
normal completion of the subprogram body, if the mode is in out or out, the
value of the formal parameter is copied back into the associated actual
parameter. For a parameter whose type is an access type, copy-in is used
for all three modes, and copy-back for the modes in out and out.
For a parameter whose type is an array, record, or task type, an
implementation may likewise achieve the above effects by copy, as for
scalar types. In addition, if copy is used for a parameter of mode out,
then copy-in is required at least for the bounds and discriminants of the
actual parameter and of its subcomponents, and also for each subcomponent
whose type is an access type. Alternatively, an implementation may achieve
these effects by reference, that is, by arranging that every use of the
formal parameter (to read or to update its value) be treated as a use of
the associated actual parameter, throughout the execution of the subprogram
call. The language does not define which of these two mechanisms is to be
adopted for parameter passing, nor whether different calls to the same
subprogram are to use the same mechanism. The execution of a program is
erroneous if its effect depends on which mechanism is selected by the
implementation.
For a parameter whose type is a private type, the above effects are
achieved according to the rule that applies to the corresponding full type
declaration.
Within the body of a subprogram, a formal parameter is subject to any
constraint resulting from the type mark given in its parameter
specification. For a formal parameter of an unconstrained array type, the
bounds are obtained from the actual parameter, and the formal parameter is
constrained by these bounds (see 3.6.1). For a formal parameter whose
declaration specifies an unconstrained (private or record) type